package org.jcubic.v.core;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.jcubic.v.core.Config.Config;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * 读取配置文件 .conf.json
 */
public class ConfigHelper {


    private static final String ENCODING = "UTF-8";
    private static String configPath;

    public static Config config;


    public static void init(String path) {
        if (StringUtils.isBlank(path)) {
            throw new RuntimeException("配置文件 [" + path + "] 不存在");
        }
        configPath = path;
        try {
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
//            String configStr = FileUtils.readFileToString(new File(loader.getResource(path).getPath()), ENCODING);
//            System.out.println(configStr);

            config = JSON.parseObject(new FileInputStream(new File(loader.getResource(path).getPath())), Config.class);


        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("读取配置文件 [" + path + "] 失败");
        }
    }

    public static Config getConfig() {
        return config;
    }


}
